{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a47d0dab",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM, MistralConfig, GPT2Config, AutoConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fa826454",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
     ]
    }
   ],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained('malaysia-ai/bpe-tokenizer')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e1fe42df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MistralConfig {\n",
       "  \"_name_or_path\": \"mesolitica/mistral-7b-4096-fpf\",\n",
       "  \"architectures\": [\n",
       "    \"MistralForCausalLM\"\n",
       "  ],\n",
       "  \"bos_token_id\": 1,\n",
       "  \"eos_token_id\": 2,\n",
       "  \"hidden_act\": \"silu\",\n",
       "  \"hidden_size\": 512,\n",
       "  \"initializer_range\": 0.02,\n",
       "  \"intermediate_size\": 2048,\n",
       "  \"max_position_embeddings\": 32768,\n",
       "  \"model_type\": \"mistral\",\n",
       "  \"num_attention_heads\": 16,\n",
       "  \"num_hidden_layers\": 8,\n",
       "  \"num_key_value_heads\": 8,\n",
       "  \"pad_token_id\": 0,\n",
       "  \"rms_norm_eps\": 1e-05,\n",
       "  \"rope_theta\": 10000.0,\n",
       "  \"sliding_window\": 4096,\n",
       "  \"tie_word_embeddings\": false,\n",
       "  \"torch_dtype\": \"bfloat16\",\n",
       "  \"transformers_version\": \"4.34.0\",\n",
       "  \"use_cache\": true,\n",
       "  \"vocab_size\": 32000\n",
       "}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config = AutoConfig.from_pretrained('mesolitica/mistral-7b-4096-fpf')\n",
    "config.pad_token_id=tokenizer.pad_token_id\n",
    "config.bos_token_id=tokenizer.bos_token_id\n",
    "config.eos_token_id=tokenizer.eos_token_id\n",
    "config.vocab_size = len(tokenizer)\n",
    "config.hidden_size=512\n",
    "config.num_hidden_layers=8\n",
    "config.num_attention_heads=16\n",
    "config.max_position_embeddings=32768\n",
    "config.intermediate_size=2048\n",
    "config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "69d6707a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MistralConfig {\n",
       "  \"_name_or_path\": \"mesolitica/mistral-7b-4096-fpf\",\n",
       "  \"architectures\": [\n",
       "    \"MistralForCausalLM\"\n",
       "  ],\n",
       "  \"bos_token_id\": 1,\n",
       "  \"eos_token_id\": 2,\n",
       "  \"hidden_act\": \"silu\",\n",
       "  \"hidden_size\": 512,\n",
       "  \"initializer_range\": 0.02,\n",
       "  \"intermediate_size\": 2048,\n",
       "  \"max_position_embeddings\": 32768,\n",
       "  \"model_type\": \"mistral\",\n",
       "  \"num_attention_heads\": 16,\n",
       "  \"num_hidden_layers\": 8,\n",
       "  \"num_key_value_heads\": 8,\n",
       "  \"pad_token_id\": 0,\n",
       "  \"rms_norm_eps\": 1e-05,\n",
       "  \"rope_theta\": 10000.0,\n",
       "  \"sliding_window\": 4096,\n",
       "  \"tie_word_embeddings\": false,\n",
       "  \"torch_dtype\": \"bfloat16\",\n",
       "  \"transformers_version\": \"4.34.0\",\n",
       "  \"use_cache\": true,\n",
       "  \"vocab_size\": 32000\n",
       "}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = AutoModelForCausalLM.from_config(config, torch_dtype = torch.bfloat16)\n",
    "model.config.torch_dtype = 'bfloat16'\n",
    "model.config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8c01403d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "64233984"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(\n",
    "param.numel() for param in model.parameters()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c28aaef1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "839cccbbde8b463ea6bf6caaa31996da",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "pytorch_model.bin:   0%|          | 0.00/128M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/huseinzol05/dummy-mistral-64M/commit/881a0c3cf08e21ea5effc2d0ea5fb8c9e9e0b35f', commit_message='Upload MistralForCausalLM', commit_description='', oid='881a0c3cf08e21ea5effc2d0ea5fb8c9e9e0b35f', pr_url=None, pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.push_to_hub('huseinzol05/dummy-mistral-64M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "60c545ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/huseinzol05/dummy-mistral-64M/commit/748cde8b21a83fdbc89b32b3e2a0ac7a6a4867fd', commit_message='Upload config', commit_description='', oid='748cde8b21a83fdbc89b32b3e2a0ac7a6a4867fd', pr_url=None, pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config.push_to_hub('huseinzol05/dummy-mistral-64M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adad3116",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
